package com.sky.demo.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.sky.demo.R;
import com.sky.demo.bitmap.ImageUtils;
import com.sky.demo.global.BaseActivity;
import com.sky.demo.utils.SPUtils;

/** 
* @ClassName: WelcomeActivity 
* @Description: 欢迎页
* @author LiBin
* @date 2015年3月26日 下午4:19:07 
*  
*/
public class WelcomeActivity extends BaseActivity {
	public SharedPreferences preferences;
	public Boolean flag;
	private Animation animation;
	private int TIME=1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
//		SPUtils.put(WelcomeActivity.this, "isfirst", true);
		flag = (Boolean) SPUtils.get(this, "isfirst", true);
		animation();
	}

	public void animation() {
		RelativeLayout layout = (RelativeLayout) findViewById(android.R.id.icon2);
		Bitmap background = ImageUtils
				.readBitMap(this, R.mipmap.welcome);
		Drawable drawable = ImageUtils.bitmapToDrawable(this, background);
		layout.setBackgroundDrawable(drawable);
		
		animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
		layout.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						// 判断程序与第几次运行，如果是第一次运行引导页面
						if (flag) {
							startActivity(new Intent(WelcomeActivity.this,
									IntroductoryActivity.class));
							overridePendingTransition(R.anim.in_from_right,
									R.anim.out_to_left);
							finish();
						} else {
								startActivity(new Intent(WelcomeActivity.this,
										MainActivity.class));
								overridePendingTransition(R.anim.in_from_right,
										R.anim.out_to_left);
								finish();
						}
					}
				}, TIME);
			}
		});
	}
}
